﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using SMALLERP.PO;
using System.Drawing.Printing;
  
namespace SMALLERP.PO
{
    public partial class Print1 : Form

    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        //AppPO POForm;
        public delegate void ChangeZXS(object sender, EventArgs e);

        public Print1()
        {
            InitializeComponent();
        }

        public event ChangeZXS ZXS;

        private void Print1_Load(object sender, EventArgs e)
        {
            
            this.textBox1.Text = "0";
            this.textBox2.Text = "0";
            this.textBox9.Text = "0";
            this.textBox7.Text = QJBL.KHJC.ToString();
            this.textBox8.Text = DateTime.Now.ToString();
            this.textBox13.Text = QJBL.poDDH.ToString();
           
            //this.textBox9.Text ="A0706001"; 
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox12.Text = textBox2.Text;
           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double xs = double.Parse(textBox1.Text);
            double dx = double.Parse(textBox2.Text);
            string xds = QJBL.poXDS.ToString();
            double xd = double.Parse(xds);
            string zxs = QJBL.poYZX.ToString();
            double zx = double.Parse(zxs);
            string yjs = QJBL.poYJS.ToString();
            double yj = double.Parse(yjs);
            string dys = QJBL.poDYS.ToString();
            double dy = double.Parse(dys);
            string yts = QJBL.poYTS.ToString();
            double yt = double.Parse(yts);
            if (textBox1.Text == "0")
            {
                try
                {
                    MessageBox.Show("箱数不能为0！","软件提示");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }

            if (textBox2.Text == "0")
            {
                try
                {
                    MessageBox.Show("每箱数量不能为0！", "软件提示");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            if ((xs * dx) > (xd + yt - yj-dy))
            {
                try
                {
                    MessageBox.Show("装箱总数不能超过可交数量！,现在还能交" + (xd + yt - yj - dy) + "", "软件提示");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            if ((xs * dx) > (xd + yt -zx))
            {
                try
                {
                    MessageBox.Show("装箱总数不能超过可装箱数！,现在还能装" + (xd + yt -zx) + "", "软件提示");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            if (checkBox1.Checked)
            {
                if(textBox7.Text=="")
               try
                {
                 MessageBox.Show("拼箱时必须先输入箱号", "软件提示");
                 return;
                }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message, "软件提示");
                   throw ex;
               }
               finally
               {
               }
            }
            //printDialog1.ShowDialog();//用默认的所有者使用通用的对话框、
            PrintDialog MySettings = new PrintDialog();
            MySettings.Document = this.printDocument1;   
            PaperSize p = null;
            foreach (PaperSize ps in MySettings.Document.PrinterSettings.PaperSizes)
            {
                if (ps.PaperName.Equals("TXM"))//这里设置纸张大小,但必须是定义好的   
                    p = ps;
            }
            MySettings.Document.DefaultPageSettings.PaperSize = p;
            if (MySettings.ShowDialog() == DialogResult.OK)
            {

                for (int i = 0; i < xs; i++)
                {
                    string strSql = null;
                    string tm1 = null;

                    strSql = "SELECT ISNULL('D'+RIGHT(CONVERT(CHAR(8),GETDATE(),112),4)+RIGHT('000'+RTRIM(cast(MAX(CAST(SUBSTRING(XA002,6,4) as bigint))+1 AS CHAR(4))),4) ,";
                    strSql += "'D'+RIGHT(CONVERT(CHAR(8),GETDATE(),112),4)+'0001') AS XH FROM COPXA ";
                    strSql += "WHERE XA002 LIKE 'D'+RIGHT(CONVERT(CHAR(8),GETDATE(),112),4)+'%'";
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);
                        sdr1.Read();
                        tm1 = sdr1["XH"].ToString();
                        if (checkBox1.Checked)
                        {
                            tm1 = textBox7.Text.ToString();
                        }
                        barcodeControl2.Text = tm1;
                        barcodeControl2.Value = tm1;
                        barcodeControl2.Refresh();
                        textBox7.Text = tm1;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                    finally
                    {
                        sdr1.Close();
                    }



                    //获取帐套数据库
                    string DataBase2 = OperatorFile.GetIniFileString("DataBase", "DataBase2", "",
                                                                  Application.StartupPath + "\\SCM.ini");

                    strSql = "INSERT INTO COPXA SELECT  '" + DataBase2 + "','" + PropertyClass.OperatorCode + "','',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'','',1,RIGHT(CONVERT(CHAR(8),GETDATE(),112)+'0000000000000000000000000000000000000000000000000'+RTRIM(ISNULL(CAST(CAST(MAX(RIGHT(RTRIM(XA001),10)) AS NUMERIC(10))+1 AS VARCHAR(10)),'1')),60),";
                    strSql += "'" + tm1 + "','" + QJBL.poDB.ToString() + "','" + QJBL.poNO.ToString() + "','" + QJBL.invcode.ToString() + "','" + QJBL.invname.ToString() + "','" + QJBL.invspec.ToString() + "','" + QJBL.CPTH.ToString() + "','" + textBox13.Text + "',''," + textBox12.Text + "," + textBox4.Text + ",0,'','','" + QJBL.poDDD.ToString() + "','" + QJBL.poDDH.ToString() + "','" + QJBL.poDXH.ToString() + "',CONVERT(CHAR(8),GETDATE(),112),'','','','N','N','N','','','','','','',0,0,0,'" + QJBL.poXH.ToString() + "','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0,'','','','','',1,'','','','' FROM COPXA WHERE XA001 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'";



                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                            //MessageBox.Show("打印成功！", "软体提示");


                        }
                        else
                        {
                            MessageBox.Show("打印失败！", "软体提示");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软体提示");
                        throw ex;
                    }

                    printPreviewDialog1.Document = this.printDocument1;//设置打印的文档
                    printPreviewDialog1.ShowDialog();//将窗体显示为模式对话框，并将当前活动窗口设置为所有者
                    //this.printDocument1.Print(); 

                }
            }
                MessageBox.Show("打印成功！", "软体提示");
                //this.FormClosed += new EventHandler(AppPO_Activated);
                ZXS(sender,e);
                Close();
           
        }

       

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {      
            e.HasMorePages=false;
            Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
            panel1.DrawToBitmap(_NewBitmap, new Rectangle(2, 2, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 2, 2, _NewBitmap.Width, _NewBitmap.Height);

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

       

       

        private void textBox8_TextChanged_1(object sender, EventArgs e)
        {

        }



        

        private void Print_Deactivate(object sender, EventArgs e)
        {
            
        }





        public EventHandler AppPO_Activated { get; set; }

        private void Print1_Deactivate(object sender, EventArgs e)
        {

        }

        private void Print1_Activated(object sender, EventArgs e)
        {

        }

        private void Print1_Load_1(object sender, EventArgs e)
        {
            this.textBox1.Text = "0";
            this.textBox2.Text = "0";
            this.textBox9.Text = "0";
            this.textBox5.Text = QJBL.invcode.ToString();
            this.textBox8.Text = QJBL.invspec.ToString();
            this.textBox13.Text = QJBL.KHJC.ToString();
            this.textBox10.Text = QJBL.poDDH.ToString();
         
                     //this.textBox9.Text ="A0706001";

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            textBox4.Text = textBox9.Text;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
            string strSql;
            strSql = "SELECT * FROM PURTD WHERE TD001='" + QJBL.poDB + "' AND TD002='" + QJBL.poNO + "' AND TD003='" + QJBL.poXH + "'";

            try
            {
                sdr1 = db.GetDataReader1(strSql);
                sdr1.Read();
                textBox14.Text = sdr1["TD009"].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            finally
            {
                sdr1.Close();
            }
            
        }

        private void textBox5_TextChanged_1(object sender, EventArgs e)
        {

        }
    }
}
